<%--                                                                       --%>
<%-- Copyright (c) 2001-2004 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries.                       --%>
<%--                                                                       --%>

<%@ page language="java" %>
<%-- <%@ page import = "com.broadvision.portal.services.microsite.Attachment" %> --%>
<%@ page import = "java.util.*" %>
<%@ page import = "org.apache.struts.util.RequestUtils" %>
<%@ page import = "com.broadvision.web.util.DateTime" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/bv-html.tld" prefix="bvhtml" %>

<bean:define id="micrositeNaviInfoParams" name="micrositeNaviInfoView" property="urlParams" scope="request" />

<script language="javascript" src="<%=request.getContextPath()%>/cs/js/date.js"></script>

<script language="javascript">
function openwin(file) {
  var win = window.open(file , "NewWindow", 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=450,height=338');
  win.focus();
}

<% 
  String nextAction = "/cs/taskAttachmentAdd.do";
  String nextPage = RequestUtils.computeURL(pageContext,
                                           null,
                                           null,
                                           nextAction,
                                           null,
                                           null,
                                           false);
  String relativeDir = "/";
  /*  + csTaskEditForm.getServiceName() 
                           + "/microsite/" 
                           + csTaskEditForm.getMicrositeId(); 
  */


%>

function onClickReset() {
  document.csTaskEditForm.reset();
}

function removeRelatedItem(OID, contentType) {
  document.csTaskEditForm.action = '<html:rewrite page="/cs/taskRelatedItemRemove.do"/>';
  document.csTaskEditForm.removeRelatedId.value = OID;
  document.csTaskEditForm.removeRelatedContentType.value = contentType;
  document.csTaskEditForm.parentContentType.value = '331'; // Task
  document.csTaskEditForm.encoding = "application/x-www-form-urlencoded";
  document.csTaskEditForm.method = "post";
  document.csTaskEditForm.submit();
}

function onClickDeleteAttachment(index, id) {
  if (confirm("<bean:message key="cs.confirm.delete.attachment"/>")) {
    document.csTaskEditForm.action =  '<html:rewrite page="/cs/taskAttachmentRemove.do"/>';
    document.csTaskEditForm.encoding = "application/x-www-form-urlencoded";
    document.csTaskEditForm.method = "post";
    document.csTaskEditForm.deleteAttIndex.value = index;
    document.csTaskEditForm.deleteAttId.value = id;
    document.csTaskEditForm.submit();
  }
}

function onClickSave() {
  if (validateForm()) {
    //document.csTaskEditForm.action = '<html:rewrite page="/cs/taskAttachmentSave.do"/>';
    //document.csTaskEditForm.encoding = "application/x-www-form-urlencoded";
    //document.csTaskEditForm.method = "post";
    document.csTaskEditForm.submit();
  }
}

function onClickButton(theForm, url) {
  theForm.action = url;
  theForm.submit();
}

function onClickDeleteTask(theForm, url) {
   if (confirm("<bean:message key="cs.tasks.confirm.delete"/>")) {
     theForm.action = url;
     theForm.submit();
   }
   else {
     return false;
   }
}

function onClickBrowseRelatable() {

  if(document.csTaskEditForm.selectedRelatableType.value < 0) return;
  var redirectUrl;
  switch(document.csTaskEditForm.selectedRelatableType.value) {
  // Messages
  case "25":
    redirectUrl = '<html:rewrite page="/cs/relatedMessage.do" name="micrositeNaviInfoView" property="urlParams" paramId="contentName" paramName="csTaskEditForm" paramProperty="name"/>';
    break;
  // documents
  case "322":
    redirectUrl = '<html:rewrite page="/cs/relatedDocument.do" name="micrositeNaviInfoView" property="urlParams" paramId="contentName" paramName="csTaskEditForm" paramProperty="name"/>';
    break;
  default:
    redirectUrl = '<html:rewrite page="/cs/categoryBrowse.do" name="micrositeNaviInfoView" property="urlParams" paramId="contentName" paramName="csTaskEditForm" paramProperty="name" />';
    if(redirectUrl.indexOf("?") > 0) {
      redirectUrl += '&amp;contentTypeId=' + document.csTaskEditForm.selectedRelatableType.value;
    }
    else {
      redirectUrl += '?contentTypeId=' + document.csTaskEditForm.selectedRelatableType.value;
    }

  }
  // The following two lines are necessary for netscape to correctly send the
  // query string
  var re = new RegExp('&amp;','gi');
  var newRedirectUrl = redirectUrl.replace(re,"&");

  var win = window.open(newRedirectUrl,'addRelatedItem', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=700,height=500');
  win.focus();

}

// Load the related items chosen in the pop-up window
function loadRelatedItems(OIDs, contentType) {
  document.csTaskEditForm.relatedItems.value = OIDs;
  document.csTaskEditForm.relatedItemsContentType.value = contentType;
  document.csTaskEditForm.action = '<html:rewrite page="/cs/taskAddRelated.do"/>'
  document.csTaskEditForm.encoding = "application/x-www-form-urlencoded";
  document.csTaskEditForm.method = "post";
  document.csTaskEditForm.submit();
}


function onFileInputFocus()
{
  return document.csTaskEditForm.newFile.focus();
}

//
// Trims leading blanks.
//
function stringLeftTrim(s)
{
   return s.replace(/^ +/, "");
}

//
// Trims trailing blanks.
//
function stringRightTrim(s)
{
   return s.replace(/ +$/, "");
}

//
// Trims leading and trailing blanks.
//
function stringTrim(s)
{
   return stringRightTrim(stringLeftTrim(s));
}

//
// Validate the user's entries.
//
function validateForm()
{
   var taskName = stringTrim(document.csTaskEditForm.name.value);

//alert('JJ inside validateForm');
   if (taskName == "")
   {
      alert("<bean:message key="cs.error.task.name"/>");
      return false;
   }

//alert('JJ after task name');
   var taskDesc = stringTrim(document.csTaskEditForm.description.value);
   if (taskDesc.length > 2000) 
   {
      alert("<bean:message key="cs.error.task.description"/>");
      return false;
   }
//alert('JJ after description');
   var groupIndex = document.csTaskEditForm.groupIds.selectedIndex;

   if (groupIndex == -1)
   {
      alert("<bean:message key="cs.error.task.read.group"/>");
      return false;	
   }
//alert('JJ after groupIndex');


   var selectedOwner = document.csTaskEditForm.ownerId.value;

   if (selectedOwner == -1 || selectedOwner == 0)
   {
      alert("<bean:message key="cs.error.task.owner"/>");
      return false;  
   }

   var startDate = getDate2("csTaskEditForm", "start")

   if (startDate == null)
   {
      alert("<bean:message key="cs.error.task.startDate"/>");
      return false;
   }
//alert('JJ after startDate');
   var dueDate = getDate2("csTaskEditForm", "due");

   if (dueDate == null)
   {
      alert("<bean:message key="cs.error.task.endDate"/>");
      return false;
   }

//alert('JJ after due Date');
   if (startDate.getTime() > dueDate.getTime())
   {
      alert("<bean:message key="cs.error.task.endDateBeforeStartDate"/>");
      return false;
   }
//alert('JJ after startDate > dueDate');

<logic:equal name="csTaskEditForm" property="completed" value="true">
   var completeDate = getDate2("csTaskEditForm", "complete");

   if (completeDate == null)
   {
      alert("<bean:message key="cs.error.task.completeDate"/>");
      return false;
   }
   
   if (startDate.getTime() > completeDate.getTime())
   {
      alert("<bean:message key="cs.error.task.completeDateBeforeStartDate"/>");
      return false;
   }
</logic:equal>

   var attachName = stringTrim(document.csTaskEditForm.attName.value);
   var attachFile = stringTrim(document.csTaskEditForm.attOriginalFileName.value);

   if (attachName != "" || attachFile != "")
   {
      alert('<bean:message key="cs.error.attachment.add.more"/>');
      return false;
   }
   
//alert('JJ before return true');
   return true;
}


function 
arrayContainsItem(array, item)
{
   var len_array = array.length;
   var i_array;

   for (i_array = 0; i_array < len_array; i_array++)
   {
      if (array[i_array] == item)
      {
         return true;
      }
   }

   return false;
}


function onChangeTaskStatus(theForm) {
  var statusIndex = theForm.taskStatus.selectedIndex;
  var status = theForm.taskStatus.options[statusIndex].value;
  theForm.action = '<html:rewrite page="/cs/taskChangeStatus.do"/>';
  theForm.method = "post";  
  theForm.submit();
}


//
// Create a dummy (timestamp) attribute to get around an IE cache problem.
//
function getTimeStamp() {
   var now = new Date;
   var timestamp = '' + now.getYear() + now.getMonth() + now.getDate() + now.getHours() + now.getMinutes() + now.getSeconds() + now.getMilliseconds();
   return timestamp;
}   


function selectOwnerPopup(theForm) {
  var groupIndex = document.csTaskEditForm.groupIds.selectedIndex;

  if (groupIndex == -1) {
    alert("<bean:message key="cs.error.task.read.group"/>");
    return false;	
  }   

  var popupURL = '<html:rewrite page="/cs/contentSelectOwner.do" name="micrositeNaviInfoParams" />';
  var timestamp = getTimeStamp();
  popupURL += '&timestamp=' + timestamp;

  var win = window.open('', "ContentSelectOwner", "width=500,height=400,scrollbars=yes,resizable=yes");
  if ( win != null) {
    if( win.opener == null) {
      win.opener = self;
    }
  }
  win.focus();
  // save the current form setting so that it can be restored later
  var oldAction = theForm.action;
  var oldTarget = theForm.target;
  // submit the form to the popup so that we can pass the groupIds to the popup
  theForm.action = popupURL;
  theForm.target = "ContentSelectOwner";   
  theForm.submit();
  theForm.action = oldAction;
  theForm.target = oldTarget;
}

function setOwner(selectedOwnerId, selectedOwnerName) {
  document.csTaskEditForm.ownerId.value = selectedOwnerId;
  document.csTaskEditForm.ownerName.value = selectedOwnerName;

}

function resetOwner(selectedOwnerId, selectedOwnerName) {
  document.csTaskEditForm.ownerId.value = -1;
  document.csTaskEditForm.ownerName.value = "";

}

</script>

<!-- Zone 5 starts here.  -->

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" 
       summary="content area">
  <tr>
    <td valign="top">
    
    <!--start microsite logo and name here-->
      <div class="pageTitle">
        <%@ include file="/cs/microsite/micrositeNaviInfo.jsp" %>
      </div> 
    <!--end microsite logo and name here-->
    
    <!--  BEGIN form { task } ----------------------------- -->

    <html:form action="/cs/taskSave.do" method="post" >
    <bvhtml:hiddenList name="micrositeNaviInfoView" property="urlParams" scope="request"/>
    <input type="hidden" name="sequenceKey" value=<bean:write name="csTaskEditForm" property="sequenceKey" /> >
    <input type="hidden" name="parentContentType" value="331">
  
    <!--start title bar here-->
    <div class="pageSubtitle">
      <span class="left">
        <logic:equal name="csTaskEditForm" property="taskId" value="0">
           <bean:message key="cs.tasks.new"/>
        </logic:equal>
        <logic:notEqual name="csTaskEditForm" property="taskId" value="0">
          <bean:define id="taskViewName" name="taskEditView" property="persistentName"/>      
	  <%String taskName = taskViewName.toString();%>
          <bean:message key="cs.edit" arg0="<%=taskName %>"/> 
        </logic:notEqual>
               
      </span>
      <span class="right">
	&nbsp;
      </span>
    </div> 
    <!--end title bar here--> 
    
    <div class="blockWrap">
    
    <!-- start Page Instructions -->
    <div class="pageInstructions">
        <logic:equal name="csTaskEditForm" property="taskId" value="0">
          <bean:message key="cs.tasks.new.note"/>
        </logic:equal>
        <logic:notEqual name="csTaskEditForm" property="taskId" value="0">
          <bean:message key="cs.tasks.edit.note"/>
        </logic:notEqual>
    </div>
    <!-- End Page Instructions -->

  <bean:define id="allPhasesList" name="taskEditView" property="allPhasesList" />
  <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary='<bean:message key="cs.tasks.new"/>'>
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.name"/>*:
      </td>
      <td valign="top" width="84%" class="labelField" nowrap>
        <html:text property="name" size="40" maxlength="80" />
     </td>
    </tr>
    <tr> 
      <td valign="top" nowrap width="16%" class="label"> 
        <bean:message key="cs.description"/>: 
      </td>
      <td valign="top" width="84%" class="labelField">
        <html:textarea property="description" cols="45" rows="4"/>
    </tr>
    <logic:equal name="micrositeNaviInfoView" property="micrositeTemplatePhaseFlag" value="true">
      <tr> 
        <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.phase"/>*:</td>
        <td valign="top" width="84%" class="labelField">
            <html:select name="csTaskEditForm" property="newPhaseId" size="1">
              <html:options name="taskEditView" collection="allPhasesList" property="id" labelProperty="name" />
            </html:select>
        </td>
      </tr>
    </logic:equal>
    <logic:equal name="micrositeNaviInfoView" property="micrositeTemplatePhaseFlag" value="false">
      <html:hidden property="phaseId"/>
    </logic:equal>
    <tr> 
      <td valign="top" width="16%" class="label">
        <bean:message key="cs.read.access.groups"/>*:<br>
      </td>
      <td valign="top" width="84%" class="labelField">
          <bean:define id="availableGroups" name="taskEditView" property="availableGroups" />
          <html:select property="groupIds" size="4" multiple="true" onchange="resetOwner()">
            <html:optionsCollection name="taskEditView" property="availableGroups"/>
          </html:select>
      </td>
    </tr>
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.owner"/>*:</td>
      <td valign="top" width="84%"class="labelField">
        <html:text name="csTaskEditForm" property="ownerName" readonly="true"/>
        <input type="hidden" name="ownerId" value=<bean:write name="csTaskEditForm" property="ownerId"/> >
        <html:button property="selectOwner" onclick="selectOwnerPopup(this.form)" styleClass="inputButton">
          <bean:message key="cs.button.select.owner"/>
        </html:button>
      </td>
    </tr>
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.priority"/>*:</td>
      <td valign="top" width="84%" class="labelField">
        <html:select name="csTaskEditForm" property="priority" size="1">
          <html:option value="0"><bean:message key="cs.high"/></html:option>
          <html:option value="1"><bean:message key="cs.medium"/></html:option>
          <html:option value="2"><bean:message key="cs.low"/></html:option>
        </html:select>
      </td>
    </tr>
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.tasks.startDate"/>:</td>
      <td valign="top" width="84%" class="labelField" nowrap>
        <bvhtml:date name="csTaskEditForm" property="start"/>
        <a href="JavaScript:datePopup('<html:rewrite page="/cs/calendar.do"/>', 'csTaskEditForm','start')"><img src="<%= request.getContextPath() %>/cs/images/icons/calendar.gif" border="0" align="absmiddle"/></a>
      <bvhtml:time name="csTaskEditForm" property="start"/>
      </td>
    </tr> 
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.tasks.dueDate"/>:</td>
      <td valign="top" width="84%" class="labelField" nowrap>
        <bvhtml:date name="csTaskEditForm" property="due"/>
        <a href="JavaScript:datePopup('<html:rewrite page="/cs/calendar.do"/>', 'csTaskEditForm','due')"><img src="<%= request.getContextPath() %>/cs/images/icons/calendar.gif" border="0" align="absmiddle"/></a>
        <bvhtml:time name="csTaskEditForm" property="due"/>
      </td>
    </tr>
    <logic:equal name="csTaskEditForm" property="completed" value="true">
    <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.tasks.completionDate"/>:</td>
      <td valign="top" width="84%" class="labelField">

        <input type="hidden" name="completed" value="true">
        <bvhtml:date name="csTaskEditForm" property="complete"/>
        <a href="JavaScript:datePopup('<html:rewrite page="/cs/calendar.do"/>', 'csTaskEditForm','complete')"><img src="<%= request.getContextPath() %>/cs/images/icons/calendar.gif" border="0" align="absmiddle"/></a>
        <bvhtml:time name="csTaskEditForm" property="complete"/>
     </td>
   </tr>
   </logic:equal>
   <logic:notEqual name="csTaskEditForm" property="taskId" value="0">   
     <tr> 
      <td valign="top" nowrap width="16%" class="label"><bean:message key="cs.status"/>*:</td>
      <td valign="top" width="84%" class="labelField"> 
        <html:select name="csTaskEditForm" property="taskStatus" size="1" onchange="return onChangeTaskStatus(this.form);">
          <html:option value="0"><bean:message key="cs.status.assigned"/></html:option>
          <html:option value="2"><bean:message key="cs.status.completed"/></html:option>
        </html:select>
      </td>   
     </tr>
   </logic:notEqual>   
  </table>

<% // Need to define the attachment form bean for the included JSP %>
<!-- Start Display attachment. -->
  
 <script language="JavaScript">
    var nextPage = '<html:rewrite page="/cs/taskAttachmentAdd.do" />';
   var attachmentDeleteAction = '<html:rewrite page="/cs/taskAttachmentDelete.do" />';

   <logic:equal name="csTaskEditForm" property="taskId" value="0">
     var nextActionAfterCancel = '/cs/taskList.do';
     var nextActionAfterReset = '/cs/taskNew.do';
   </logic:equal>
   <logic:notEqual name="csTaskEditForm" property="taskId" value="0">
     var nextActionAfterCancel = '/cs/taskView.do';
     var nextActionAfterReset = '/cs/taskEdit.do';
   </logic:notEqual>
 </script>
    
 <bean:define id="formBeanUsed" name="csTaskEditForm" toScope="request" />
 <%@ include file="/cs/microsite/attachment/attachmentEdit.jsp" %>
 <!-- end of attachments secion -->

  <!--end main content area here-->    
    
    <!--start cancel, reset, save bar area here-->

    <div class="pageButtonWrap" align="right">
          <input type="button" class="pageButton" name="newBtn"
                 value='<bean:message key="button.reset"/>'
                 titleKey="button.new"
                 onclick="resetAttachment(this.form)"> 
	&nbsp;&nbsp;
      <input type="button" class="pageButton" name="cancelBtn"
             value='<bean:message key="button.cancel"/>'
             titleKey="button.cancel"
             onclick="onClickButton(this.form, '<html:rewrite page="/cs/taskList.do"/>')">
             
      <input type="button"  class="pageButton" name="saveBtn"
             titleKey="button.save" onclick="onClickSave()"
             value='<bean:message key="button.save"/>'>
             
    </div>          
    <!--end cancel, reset, save bar area here-->
    </html:form>
    
    </div>
    
    </td>
  </tr>
</table>
<!--end zone 5 content here -->
